/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.project.web.projectprocess;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.utils.Constant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.project.entity.projectprocess.ProjectProcess;
import com.idenger.modules.project.service.projectprocess.ProjectProcessService;

/**
 * 项目推进Controller
 * @author chenlin
 * @version 2018-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/projectprocess/projectProcess")
public class ProjectProcessController extends BaseController {

	@Autowired
	private ProjectProcessService projectProcessService;
	
	@ModelAttribute
	public ProjectProcess get(@RequestParam(required=false) String id) {
		ProjectProcess entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectProcessService.get(id);
		}
		if (entity == null){
			entity = new ProjectProcess();
		}
		return entity;
	}
	
	@RequiresPermissions("project:projectprocess:projectProcess:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectProcess projectProcess, HttpServletRequest request, HttpServletResponse response, Model model) {

		projectProcess.setProjectId(request.getParameter("projectId"));
		Page<ProjectProcess> page = projectProcessService.findPage(new Page<ProjectProcess>(request, response), projectProcess); 
		model.addAttribute("page", page);
		model.addAttribute("projectId",request.getParameter("projectId"));
		return "modules/project/projectprocess/projectProcessList";
	}

	@RequiresPermissions("project:projectprocess:projectProcess:view")
	@RequestMapping(value = "form")
	public String form( HttpServletRequest request,ProjectProcess projectProcess, Model model) {
		model.addAttribute("projectProcess", projectProcess);
		if(StringUtils.isNotBlank(projectProcess.getProjectId())){
			model.addAttribute("projectId",projectProcess.getProjectId());
		}else{
			model.addAttribute("projectId",request.getParameter("projectId"));
		}
		return "modules/project/projectprocess/projectProcessForm";
	}

	@RequiresPermissions("project:projectprocess:projectProcess:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request, ProjectProcess projectProcess, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, projectProcess)){
			return form(request,projectProcess, model);
		}
		String projectId = request.getParameter("projectId");
		projectProcess.setProjectId(projectId);
		projectProcess.setDeleteFlag(Constant.NORMAL_STATUS);
		projectProcessService.save(projectProcess);
		addMessage(redirectAttributes, "保存项目推进成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectprocess/projectProcess/?repage&projectId="+projectId;
	}
	
	@RequiresPermissions("project:projectprocess:projectProcess:edit")
	@RequestMapping(value = "delete")
	public String delete(ProjectProcess projectProcess, RedirectAttributes redirectAttributes) {
		projectProcessService.delete(projectProcess);
		addMessage(redirectAttributes, "删除项目推进成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectprocess/projectProcess/?repage";
	}

}